﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LanguageInfo.Factory;
using LanguageInfo.IDAL;
using LanguageInfo.Common;
using System.Data;
using System.Text.RegularExpressions;
using LanguageInfo.Common.Utilities;

namespace LanguageInfo.BLL
{
    public class WordsBLL
    {
        IWordsDAL narInfoDAL = LanguageInfoFactory.CreateWordsDAL();
        public DataSet GetWordsInfoTopFrm(string strInfo)
        {
            return narInfoDAL.WordsInfoTop(strInfo); 
        }
        public string GetWordsInfoTop(string strInfo)
        {
            string strJosn = string.Empty;
            DataSet ds = narInfoDAL.WordsInfoTop(strInfo);
            if (ds != null || ds.Tables.Count > 0)
            {
                List<string> objStrList = new List<string>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    objStrList.Add(ds.Tables[0].Rows[i]["HEADWORD"].ToString());
                }
                strJosn = JsonHelper.Serializer(objStrList);
            }
            return strJosn;
        }
        public DataSet WordsInfoById(string strId)
        {
            return narInfoDAL.WordsInfoById(strId);
        }
        public DataSet WordsInfoByWordIdAndPrimarySense(string WordsId, string PrimarySense)
        {
            return narInfoDAL.WordsInfoByWordIdAndPrimarySense(WordsId, PrimarySense);
        }

        public void InsertEditWordInfo(DataSet AddTableds)
        {
            narInfoDAL.InsertEditWordInfo(AddTableds);
        }
        public void UpdateEditWordInfo(DataSet UpdateByIdOrOrderDs)
        {
            narInfoDAL.UpdateEditWordInfo(UpdateByIdOrOrderDs);
        }

        public void InsertWordInfo(DataSet ds)
        {
            narInfoDAL.InsertWordInfo(ds);
        }

        public void DelWordInfoByWordIdAndPrimarySense(string WordsId, string PrimarySense)
        {
            narInfoDAL.DelWordInfoByWordIdAndPrimarySense(WordsId, PrimarySense);
        }

        public void UpdateWordInfo(DataSet UpdateByIdOrOrderDs)
        {
            narInfoDAL.UpdateWordInfo(UpdateByIdOrOrderDs);
        }
        public void DelWordInfoById(int intId)
        {
            narInfoDAL.DelWordInfoById(intId);
        }

        public string GetWordsInfo(string strInfo, int PageSize, int pageNo)
        {
            string strJosn = string.Empty;
            try
            {
                int startIndex = 0;
                int endIndex = 0;
                int start = 0;
                if (pageNo > 0)
                    start = (pageNo - 1) * PageSize;
                startIndex = start;
                endIndex = start + PageSize;
                FlexigridObject objJosn = new FlexigridObject();
                DataSet ds = narInfoDAL.WordsInfo(strInfo, startIndex, endIndex);
                if (ds != null && ds.Tables.Count > 0)
                {
                    objJosn.total = ds.Tables[0].Rows.Count;
                    objJosn.page = pageNo;

                    DataTable dtInfo = ds.Tables[0];
                    List<FlexigridRow> objListRow = new List<FlexigridRow>();
                    for (int i = 0; i < dtInfo.Rows.Count; i++)
                    {
                        FlexigridRow objRow = new FlexigridRow();
                        objRow.id = dtInfo.Rows[i]["WordsId"].ToString();
                        List<string> objListStr = new List<string>();
                        objListStr.Add(objRow.id);
                        string strHref = "<a href=\"javascript:void(0)\" onclick=\"tb_SelectInfo('{0}','{1}')\">{1}</a>";
                        strHref = string.Format(strHref, objRow.id, dtInfo.Rows[i]["Headword"].ToString());
                        objListStr.Add(strHref);
                        objRow.cell = objListStr;
                        objListRow.Add(objRow);
                    }
                    objJosn.rows = objListRow;
                    strJosn = JsonHelper.Serializer(objJosn);
                }
                return strJosn;
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        public string WordsInfoByWordsId(string strInfo, int PageSize, int pageNo)
        {
            string strJosn = string.Empty;
            try
            {
                int startIndex = 0;
                int endIndex = 0;
                int start = 0;
                if (pageNo > 0)
                    start = (pageNo - 1) * PageSize;
                startIndex = start;
                endIndex = start + PageSize;
                FlexigridObject objJosn = new FlexigridObject();
                DataSet ds = narInfoDAL.WordsInfoByWordsId(strInfo);
                if (ds != null && ds.Tables.Count > 0)
                {
                    objJosn.total = ds.Tables[0].Rows.Count;
                    objJosn.page = pageNo;

                    DataTable dtInfo = ds.Tables[0];
                    List<FlexigridRow> objListRow = new List<FlexigridRow>();
                    for (int i = 0; i < dtInfo.Rows.Count; i++)
                    {
                        FlexigridRow objRow = new FlexigridRow();
                        objRow.id = dtInfo.Rows[i]["WordsId"].ToString() + i;
                        List<string> objListStr = new List<string>();
                        objListStr.Add(dtInfo.Rows[i]["WordsId"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["PrimarySense"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Annotation"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Variety"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Note"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Semanteme"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Lable"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["ConceptStru"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["StructSemantics"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["GrammarNote"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["ModelExample"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["CollocateElement"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["SemanticRelation"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["CulturalInformation"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Synonym"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Antonym"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["DownHyponym"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["UpHyponym"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Homograph"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Homonym"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["FamilyWords"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["Compound"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["LanguageChunks"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["FreqCode"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["FreqTransACode"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["CreateTime"].ToString());
                        objListStr.Add(dtInfo.Rows[i]["UserName"].ToString());

                        objRow.cell = objListStr;
                        objListRow.Add(objRow);
                    }
                    objJosn.rows = objListRow;
                    strJosn = JsonHelper.Serializer(objJosn);
                }
                return strJosn;
            }
            catch (Exception ex)
            {
                LoginErr.log(ex);
                throw;
            }
        }

        public DataSet WordsInfoTreeById(string strId)
        {
            return narInfoDAL.WordsInfoAttrTreeById(strId);
        }

        public DataSet WordsInfoSensesTreeById(string strId)
        {
            return narInfoDAL.WordsInfoSensesTreeById(strId);
        }

        public DataSet WordsInfoAttrInfoById(string strId)
        {
            return narInfoDAL.WordsInfoAttrInfoById(strId);
        }
    }
}
